package com.ksy.common.image;

import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.ksy.common.R;

/**
 * Created by Ksy.
 */

public class XImageView extends RelativeLayout {
    private String url;
    private PhotoView realView;
    private LinearLayout moneyView;
    private ImageView loadingView;
    private LinearLayout errorView;

    public XImageView(Context context) {
        this(context, null);
    }

    public XImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public void setRealViewOnClickListener(OnClickListener realViewOnClickListener) {
        this.realViewOnClickListener = realViewOnClickListener;
    }

    OnClickListener realViewOnClickListener;

    public XImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.common_layout_ximageview, this, true);
        realView = findViewById(R.id.realView);
        realView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (realViewOnClickListener != null) {
                    realViewOnClickListener.onClick(v);
                }
            }
        });
        moneyView = findViewById(R.id.moneyView);
        loadingView = findViewById(R.id.loadingView);
        errorView = findViewById(R.id.errorView);
        final ValueAnimator animator = ValueAnimator.ofInt(0, 360);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                loadingView.setRotation(value);
            }
        });
        animator.setDuration(3000);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
        load(url);
    }

    public void load(String url) {
        showLoadingView();
        this.url = url;
        if (TextUtils.isEmpty(url)) {
            showError();
            return;
        }
        //.transform(new BlurTransformation(getBaseActivity(), 25, 8))
        Glide.with(getContext()).load(url).into(realView);
        showRealView();
//        ImageLoader.getInstance().load(getBaseActivity(), url).noPlaceholder(,realView, new ImageLoadCallback() {
//            @Override
//            public void onSuccess() {
//                showRealView();
//            }
//
//            @Override
//            public void onError() {
//                showError();
//            }
//        });
    }

    private void showRealView() {
        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (moneyView != null) {
            moneyView.setVisibility(GONE);

        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (realView != null) {
            realView.setVisibility(VISIBLE);
        }
    }

    private void showLoadingView() {

        if (moneyView != null) {
            moneyView.setVisibility(GONE);

        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (realView != null) {
            realView.setVisibility(GONE);
        }
        if (loadingView != null) {
            loadingView.setVisibility(VISIBLE);
        }
    }

    private void showError() {
        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (realView != null) {
            realView.setVisibility(GONE);
        }
        if (moneyView != null) {
            moneyView.setVisibility(GONE);

        }
        if (errorView != null) {
            errorView.setVisibility(VISIBLE);
        }
    }
}
